'use strict';

const { Controller } = require('egg');

class AdminController extends Controller {
  // 注册用户
  async createUser () {
    const { ctx } = this;
    const { account, password } = ctx.request.body;
    const payload = this.ctx.request.body;
    var data;
    if (!account || !password) {
      ctx.body = {
        code: 500,
        msg: '用户名密码不能为空',
        data: null
      };
      return;
    }
    let message = '';
    // console.log(typeof account);
    const res = await ctx.app.mysql.query(
      `select * from admin where account='${account}'`
    );
    // console.log(res);
    if (res.length !== 0) {
      message = '用户已存在';
      ctx.body = {
        code: 500,
        message,
        data
      };
    } else {
      const insertresult = await ctx.service.admin.createUser(payload);
      if (!insertresult) {
        message = '注册失败';
        ctx.body = {
          code: 500,
          message,
          data
        };
      } else {
        message = '注册成功';
        data = insertresult;
        ctx.body = {
          code: 200,
          message,
          data
        };
      }
    }

  }
  // 管理员登陆
  async login () {
    const { ctx } = this;
    // post请求传来的参数
    const { account, password } = ctx.request.body;
    let message = '',
      data = {};
    // 判断数据库里面是否存在该用户
    const user = JSON.parse(JSON.stringify(await ctx.app.mysql.query(`select * from admin where account='${account}'`)));
    if (user.length == 0) {
      message = '用户不存在';
      ctx.body = {
        code: 500,
        message,
        data,
      };
    } else if (password !== user[0].password) {
      message = '密码错误';
      ctx.body = {
        code: 500,
        message,
        data,
      };
    } else {
      message = '登录成功';
      const token = ctx.app.jwt.sign({ account: user[0].account }, this.app.config.jwt.secret, { expiresIn: "24h" });//h、m、s分别对应时分秒
      data = { id: user[0].id, token: token, userInfo: user[0] };
      ctx.body = {
        code: 200,
        message,
        data,
      };
    }

  }
  // 展示后台用户
  async showAdminUser () {
    const ctx = this.ctx;
    try {
      var total = 0;
      var list;
      list = await this.app.mysql.query(
        `select * from admin`
      );
      // console.log(JSON.parse(JSON.stringify(total)));
      const res = list;
      ctx.body = {
        code: 200,
        mag: 'success',
        data: res,
        total: JSON.parse(JSON.stringify(total))[0]
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: 'fail',
        data: null
      };
    }
  }
  // 更新后台用户数据
  async editAdminUser () {
    const { ctx } = this;
    // console.log(ctx.request.body);
    const { id, account, password, role } = ctx.request.body;
    try {
      const result = await ctx.service.admin.editAdminUser(id, account, password, role);
      ctx.body = {
        code: 200,
        msg: '修改成功',
        data: null
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '修改失败',
        data: null
      };
    }
  }
  // 展示手机用户
  async showUser () {
    const ctx = this.ctx;
    const { page, pageSize, account } = ctx.query;
    try {
      var total = 0;
      var list;
      list = await this.app.mysql.query(
        `select * from user where account like "%${account}%" limit ${(page - 1) * pageSize},${pageSize}`
      );
      if (account == '') {
        total = await this.app.mysql.query(
          `select count(1) from user`
        );
      } else {
        total = await this.app.mysql.query(
          `select found_rows()`
        );
      }
      // console.log(JSON.parse(JSON.stringify(total)));
      const res = list;
      ctx.body = {
        code: 200,
        mag: 'success',
        data: res,
        total: JSON.parse(JSON.stringify(total))[0]
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: 'fail',
        data: null
      };
    }
  }
  // 更新手机用户数据
  async editUser () {
    const { ctx } = this;
    // console.log(ctx.request.body);
    const { id, account, password, phone, role } = ctx.request.body;
    try {
      const result = await ctx.service.admin.editUser(id, account, password, phone, role);
      ctx.body = {
        code: 200,
        msg: '修改成功',
        data: null
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '修改失败',
        data: null
      };
    }
  }
  // 删除手机用户
  async deleteUser () {
    const { ctx } = this;
    const { id } = ctx.query;
    try {
      const result = ctx.service.admin.deleteUser(id);
      ctx.body = {
        code: 200,
        message: '成功',
        data: true
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: '失败',
        data: false
      };
    }
  }
  // 删除后台用户
  async deleteAdminUser () {
    const { ctx } = this;
    const { id } = ctx.query;
    try {
      const result = ctx.service.admin.deleteAdminUser(id);
      ctx.body = {
        code: 200,
        message: '成功',
        data: true
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: '失败',
        data: false
      };
    }
  }
  // 后台展示求助信息
  async manageShowHelp () {
    const ctx = this.ctx;
    const { page, pageSize, drugInfo } = ctx.query;
    console.log(drugInfo);
    try {
      var total = 0;
      var list;
      list = await this.app.mysql.query(
        `select * from needDrug where drugInfo like "%${drugInfo}%" limit ${(page - 1) * pageSize},${pageSize}`
      );
      if (drugInfo == '') {
        total = await this.app.mysql.query(
          `select count(1) from needDrug`
        );
      } else {
        total = await this.app.mysql.query(
          `select found_rows()`
        );
      }
      // console.log(JSON.parse(JSON.stringify(total)));
      const res = list;
      ctx.body = {
        code: 200,
        mag: 'success',
        data: res,
        total: JSON.parse(JSON.stringify(total))[0]
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: 'fail',
        data: null
      };
    }
  }
  // 展示帮助
  async manageShowProHelp () {
    const ctx = this.ctx;
    const { page, pageSize, drugInfo } = ctx.query;
    // console.log(drugInfo);
    try {
      var total = 0;
      var list;
      list = await this.app.mysql.query(
        `select * from provideDrug where proDrugInfo like "%${drugInfo}%" limit ${(page - 1) * pageSize},${pageSize}`
      );
      if (drugInfo == '') {
        total = await this.app.mysql.query(
          `select count(1) from provideDrug`
        );
      } else {
        total = await this.app.mysql.query(
          `select found_rows()`
        );
      }
      // console.log(JSON.parse(JSON.stringify(total)));
      const res = list;
      ctx.body = {
        code: 200,
        mag: 'success',
        data: res,
        total: JSON.parse(JSON.stringify(total))[0]
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: 'fail',
        data: null
      };
    }
  }
  // 审核求助
  async examineHelp () {
    const { ctx } = this;
    // console.log(ctx.request.body);
    const { id, isSolve, isEamine } = ctx.request.body;
    try {
      const result = await ctx.service.admin.editHelp(id, isSolve, isEamine);
      ctx.body = {
        code: 200,
        msg: '修改成功',
        data: null
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '修改失败',
        data: null
      };
    }
  }
  // 审核帮助
  async examineProHelp () {
    const { ctx } = this;
    // console.log(ctx.request.body);
    const { id, isSolve, isEamine } = ctx.request.body;
    try {
      const result = await ctx.service.admin.examineProHelp(id, isSolve, isEamine);
      ctx.body = {
        code: 200,
        msg: '修改成功',
        data: null
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '修改失败',
        data: null
      };
    }
  }
  // 删除求助
  async delHelp () {
    const { ctx } = this;
    const { id } = ctx.query;
    try {
      const result = ctx.service.admin.delHelp(id);
      ctx.body = {
        code: 200,
        message: '成功',
        data: true
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: '失败',
        data: false
      };
    }
  }
  // 删除帮助
  async delProHelp () {
    const { ctx } = this;
    const { id } = ctx.query;
    try {
      const result = ctx.service.admin.delProHelp(id);
      ctx.body = {
        code: 200,
        message: '成功',
        data: true
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: '失败',
        data: false
      };
    }
  }
}

module.exports = AdminController;
